package com.shen.library_nettysocket

import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

/**
 * 线程池工厂，负责重连和心跳线程调度
 */
class ExecutorServiceFactory {

    /** 管理线程组，负责重连 */
    private var bossPool: ExecutorService? = null
    /** 工作线程组，负责心跳 */
    private var workPool: ExecutorService? = null

    /**
     * 初始化「boss线程池」
     */
    @Synchronized
    fun initBossLoopGroup() {
        initBossLoopGroup(1)
    }

    /**
     * 初始化「boss线程池」
     * 重载 -- [size]线程池大小
     */
    @Synchronized
    fun initBossLoopGroup(size: Int) {
        destroyBossLoopGroup()
        bossPool = Executors.newFixedThreadPool(size)
    }

    /**
     * 初始化work线程池
     */
    @Synchronized
    fun initWorkLoopGroup() {
        initWorkLoopGroup(1)
    }

    /**
     * 初始化work线程池
     * 重载 -- [size]线程池大小
     */
    @Synchronized
    fun initWorkLoopGroup(size: Int) {
        destroyWorkLoopGroup()
        workPool = Executors.newFixedThreadPool(size)
    }

    /**
     * 执行·boss任务[r]
     */
    fun execBossTask(r: Runnable?) {
        if (bossPool == null) {
            initBossLoopGroup()
        }
        bossPool?.execute(r)
    }

    /**
     * 执行·work任务[r]
     */
    fun execWorkTask(r: Runnable?) {
        if (workPool == null) {
            initWorkLoopGroup()
        }
        workPool?.execute(r)
    }

    /**
     * 释放boss线程池
     */
    @Synchronized
    fun destroyBossLoopGroup() {
        if (bossPool != null) {
            try {
                bossPool!!.shutdownNow()
            } catch (t: Throwable) {
                t.printStackTrace()
            } finally {
                bossPool = null
            }
        }
    }

    /**
     * 释放work线程池
     */
    @Synchronized
    fun destroyWorkLoopGroup() {
        if (workPool != null) {
            try {
                workPool!!.shutdownNow()
            } catch (t: Throwable) {
                t.printStackTrace()
            } finally {
                workPool = null
            }
        }
    }

    /**
     * 释放所有线程池
     */
    @Synchronized
    fun destroy() {
        destroyBossLoopGroup()
        destroyWorkLoopGroup()
    }
}